//
//  AlertPasswordViewController.swift
//  hunan_ios_ldnydfuz
//
//  Created by yzd on 2019/5/22.
//  Copyright © 2019 ldnsm. All rights reserved.
//



import UIKit
class AlertPasswordViewController: UIViewController,UITextFieldDelegate {
    

    // ---------------内部属性-------------------
      public var tap: ((String) -> Void)? = nil
    // 密码位数
    fileprivate let passwordCount: Int = 6
    // 设置方块格高
    fileprivate let passwordHeight: CGFloat = 50
    // 密码圆点大小
    fileprivate let roundHW = 10
    // Leading
    fileprivate let leading: CGFloat = 5
    // Top
    fileprivate let top: CGFloat = kScreenHeight - 50
    // 密码输入完成后的回调
    required init() {
        super.init(nibName: nil, bundle: nil)
        view.backgroundColor = UIColor.clear
        self.providesPresentationContextTransitionStyle = true
        self.definesPresentationContext = true
        self.modalPresentationStyle = .custom
        // Setup TextField
        setupTextField()
        // Config TextField
        configTextField()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        UIView.animate(withDuration: 0.25) {
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
   
    
    lazy var textField: UITextField = {
        let el = UITextField.init()
        el.textColor = UIColor.clear
        el.tintColor = UIColor.clear
        el.autocapitalizationType = .none
        el.keyboardType = .numberPad
        el.layer.borderColor = UIColor.groupTableViewBackground.cgColor
        el.layer.borderWidth = 1.0 / UIScreen.main.scale
        el.delegate = self
        el.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        el.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
        return el
    }()
    lazy var label: UIView = {
        let el = UILabel()
        el.textAlignment = .center
        el.text = "请输入支付密码"
        el.font = UIFont.little
        el.textColor = UIColor.colBlack
        return el
    }()
    lazy var textView: UIView = {
        let el = UIView(frame:CGRect.init(x: 0, y: top - 40, width: screenWidth, height: passwordHeight + 40))
        el.backgroundColor = #colorLiteral(red: 0.8235294118, green: 0.8274509804, blue: 0.8549019608, alpha: 1)
        return el
    }()
    lazy var overVeiw: UIView = {
        let el = UIView(frame: CGRect(x: 0, y: 0, width:screenWidth, height: screenHeight))
        el.backgroundColor = UIColor.black
        el.alpha = 0.5
        return el
    }()
   
    var dotArray: Array<UIView> = []
    

    // Setup
    func setupTextField() {
        view.addSubview(overVeiw)
         view.addSubview(textView)
         view.addSubview(label)
        label.snp.makeConstraints{
            $0.left.right.equalToSuperview()
            $0.top.equalTo(textView.snp.top).offset(size10)
        }
        view.addSubview(textField)
        // 密码需要宽度
        let textFieldWidth = UIScreen.main.bounds.width-leading*2
        textField.frame = CGRect.init(x: leading, y: top, width: textFieldWidth, height: passwordHeight)
    }
    
    // Config
    func configTextField() {
        let textFieldWidth = UIScreen.main.bounds.width-leading*2
        let width = Int(textFieldWidth) / passwordCount
        // 分割线
        for i in 0..<passwordCount-1 {
            let line = UIView.init(frame: CGRect.init(x: CGFloat(Int(leading)+(i+1)*width), y: top, width: 1.0 / UIScreen.main.scale, height: passwordHeight))
            line.backgroundColor = UIColor.groupTableViewBackground
            view.addSubview(line)
        }
        
        // 点
        for i in 0..<passwordCount {
            let dot = UIView.init(frame: CGRect.init(x: CGFloat(Int(leading)+(width-passwordCount)/2+i*width), y: top+CGFloat((Int(passwordHeight)-roundHW)/2), width: CGFloat(roundHW), height: CGFloat(roundHW)))
            dot.backgroundColor = UIColor.black
            dot.layer.cornerRadius = CGFloat(roundHW/2)
            dot.clipsToBounds = true
            dot.isHidden = true
            view.addSubview(dot)
            
            dotArray.append(dot)
        }
    }
    
    // UITextFieldDelegate
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == "\n" {
            textField.resignFirstResponder()
            return false
        }else if string.count == 0 {
            return true
        }else if (textField.text?.count)! >= passwordCount {
            return false
        }else{
            return true
        }
    }
    
    @objc func textFieldDidChange() {
        for dot in dotArray {
            dot.isHidden = true
        }
        
        for i in 0..<(textField.text?.count)! {
            dotArray[i].isHidden = false
        }
        
        if (textField.text?.count)! == passwordCount {
            // 失去第一响应
            textField.resignFirstResponder()
            // 检查回调
            self.tap?(textField.text!)
        }
    }
}
extension AlertPasswordViewController {
    
    func sheetViewDismiss() {
        UIView.animate(withDuration: 0.25, animations: {
            var frame = self.textField.frame
            frame.origin.y = kScreenHeight
            self.textField.frame = frame
            self.overVeiw.alpha = 0
            
        }) { (_) in
            self.dismiss(animated: false, completion: nil)
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        textField.resignFirstResponder()
        sheetViewDismiss()
    }
}


